package edu.spec.wechat.service.impl;

import edu.spec.pms.dao.iface.IStudentDao;
import edu.spec.pms.model.student.Student;
import edu.spec.wechat.dao.iface.ISignInDao;
import edu.spec.wechat.dao.iface.ISignInSumDao;
import edu.spec.pms.model.student.SignIn;
import edu.spec.pms.model.student.SignInSum;
import edu.spec.wechat.service.iface.ISignInService;
import edu.spec.wechat.web.WechatContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * Created by MadFrog on 2016/10/29.
 */
@Service("signInService")
public class SignInService implements ISignInService {

    @Autowired
    private ISignInSumDao signInSumDao;
    @Autowired
    private ISignInDao signInDao;
    @Autowired
    private IStudentDao studentDao;

    @Override
    public String signIn(Map<String, Object> map) {
        String openId = (String) map.get("FromUserName");
        SignInSum sum = signInSumDao.loadByOpenId(openId);
        Date lastSignInDate = sum.getLastSignInDate();
        //无效签到
        if(lastSignInDate.equals(new Date())){
            return "今天您已经签到，一天之内签到一次即可，请不要重复签到。";
        }
        /**
         * 有效签到
         * 0.判断地点是否一致
         * 1.追加SignIn记录
         * 2.SignInSum更新
         */
        //TODO
        SignIn si = new SignIn();
        si.setOpenId(openId);
        Student stu = WechatContext.getInstance().getWechatUser(openId);
        si.setStudentId(stu.getId());
        si.setSignInDate(new Date());
        signInDao.add(si);

        return "签到成功";
    }
}
